package com.ruoyi.product.controller;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.product.domain.CartItemVO;
import com.ruoyi.product.domain.CartVO;
import com.ruoyi.product.domain.JsonData;
import com.ruoyi.product.request.CartItemRequest;
import com.ruoyi.product.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {


    @Autowired
    private CartService cartService;

    @RequestMapping("/add")
    public R addCart(@RequestBody CartItemRequest request){
        System.out.println("request :"+ request);
        cartService.addToCart(request);
        return R.ok();
    }

    /**
     * 用于订单服务，确认订单，获取对应的商品项详情信息
     *
     * 会清空购物车的商品数据
     * @param productIdList
     * @return
     */
    @PostMapping("confirm_order_cart_items")
    public JsonData confirmOrderCartItems(@RequestBody List<String> productIdList){
        List<CartItemVO> cartItemVOS = cartService.confirmOrderCartItems(productIdList);
        return JsonData.buildSuccess(cartItemVOS);

    }
    @GetMapping("/mycart")
    public R findMyCart(){

        CartVO cartVO = cartService.getMyCart();
        System.out.println("res.data.cartItems: "+cartVO);
        HashMap resultMap = new HashMap();
        resultMap.put("cartItems",cartVO.getCartItems());
        resultMap.put("totalNum",cartVO.getTotalNum());
        resultMap.put("totalAmount",cartVO.getTotalAmount());
        resultMap.put("realPayAmount",cartVO.getRealPayAmount());
        resultMap.put("productIds",cartVO.getProductIds());

        return R.ok(resultMap);
    }
}
